package com.game.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
        import com.game.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.game.business.mapper.GroupMessageMapper;
import com.game.business.domain.GroupMessage;
import com.game.business.service.IGroupMessageService;

/**
 * 群聊消息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-15
 */
@Service
public class GroupMessageServiceImpl extends ServiceImpl<GroupMessageMapper,GroupMessage>  implements IGroupMessageService
{
    @Autowired
    private GroupMessageMapper groupMessageMapper;

    /**
     * 查询群聊消息
     *
     * @param id 群聊消息主键
     * @return 群聊消息
     */
    @Override
    public GroupMessage selectGroupMessageById(Long id)
    {
        return groupMessageMapper.selectGroupMessageById(id);
    }

    /**
     * 查询群聊消息列表
     *
     * @param groupMessage 群聊消息
     * @return 群聊消息
     */
    @Override
    public List<GroupMessage> selectGroupMessageList(GroupMessage groupMessage)
    {
        return groupMessageMapper.selectGroupMessageList(groupMessage);
    }

    /**
     * 新增群聊消息
     *
     * @param groupMessage 群聊消息
     * @return 结果
     */
    @Override
    public int insertGroupMessage(GroupMessage groupMessage)
    {
                groupMessage.setCreateTime(DateUtils.getNowDate());
            return groupMessageMapper.insertGroupMessage(groupMessage);
    }

    /**
     * 修改群聊消息
     *
     * @param groupMessage 群聊消息
     * @return 结果
     */
    @Override
    public int updateGroupMessage(GroupMessage groupMessage)
    {
                groupMessage.setUpdateTime(DateUtils.getNowDate());
        return groupMessageMapper.updateGroupMessage(groupMessage);
    }

    /**
     * 批量删除群聊消息
     *
     * @param ids 需要删除的群聊消息主键
     * @return 结果
     */
    @Override
    public int deleteGroupMessageByIds(Long[] ids)
    {
        return groupMessageMapper.deleteGroupMessageByIds(ids);
    }

    /**
     * 删除群聊消息信息
     *
     * @param id 群聊消息主键
     * @return 结果
     */
    @Override
    public int deleteGroupMessageById(Long id)
    {
        return groupMessageMapper.deleteGroupMessageById(id);
    }


    /**
     * 获取群聊最后一条消息
     *
     */
    @Override
    public GroupMessage getLastGroupMessage(String groupId) {
        QueryWrapper<GroupMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.eq("group_id",groupId);
        queryWrapper.orderByDesc("create_time");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }
}
